package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.db.domain.MarketCouponUserExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

public class AdminCouponServiceImpl implements AdminCouponService {
    @Override
    public List<MarketCoupon> list(String page, String limit, String sort, String order, String name, String type, String status) {
        SqlSession session= MybatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(!StringUtils.isEmpty(type)){
            criteria.andTypeEqualTo(Short.valueOf(type));
        }
        if(!StringUtils.isEmpty(status)){
            criteria.andTypeEqualTo(Short.valueOf(status));
        }
        marketCouponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
        session.commit();
        session.close();
        return marketCoupons;
    }

    @Override
    public int delete(Integer id) {
        SqlSession session= MybatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);

        int i = couponMapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
        return i;
    }

    @Override
    public int update(MarketCoupon marketCoupon) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);
        int i = couponMapper.updateByPrimaryKey(marketCoupon);
        session.commit();
        session.close();
        return i;
    }

    @Override
    public List<MarketCouponUser> listuser(String page, String limit, String sort, String order, String couponId, String status) {
        SqlSession session= MybatisUtil.getSession();
        MarketCouponUserMapper couponUserMapper = session.getMapper(MarketCouponUserMapper.class);
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        if(!StringUtils.isEmpty(couponId)){
            criteria.andCouponIdEqualTo(Integer.valueOf(couponId));
        }
        if(!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(Short.valueOf(status));
        }
        marketCouponUserExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(marketCouponUserExample);
        session.commit();
        session.close();
        return marketCouponUsers;
    }

    @Override
    public MarketCoupon insert(MarketCoupon marketCoupon) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);
        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setUpdateTime(LocalDateTime.now());
        couponMapper.insert(marketCoupon);
        session.commit();
        session.close();
        return marketCoupon;
    }

}
